#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys

execfile(os.path.join(sys.path[0], "framework.py"))

#from oriolus.docker.scripts.loader import Loader, Suite
from oriolus.docker.scripts.runner import Runner, RunnerError
from oriolus.pith.packets.scriptreader import ScriptReader
from oriolus.pith.packets.script import Script

from unittest import TestCase, TestLoader, TextTestRunner

class TestRunner(TestCase):
    
    def setUp(self):
        self.__base_dir = os.path.join(".","test_runner")
        self.__temporary_resources = []
        if not os.path.exists(self.__base_dir):
            os.mkdir(self.__base_dir)
        
    def tearDown(self):
        for resource in self.__temporary_resources:
            resource_path = os.path.join(self.__base_dir, resource)
            if os.path.exists(resource_path):
                os.remove(resource_path)
                
        if os.path.exists(self.__base_dir):
            os.rmdir(self.__base_dir)
        
    def test_run_script(self):
        test_file_name = "test_file.txt"
        test_file_path = os.path.join(os.path.join(self.__base_dir, test_file_name))
        self.__temporary_resources.append(test_file_name)
        
        str = "file('%s','w').close()" % os.path.abspath(test_file_path)
        str = str.replace("\\", "/")
        script_name = "test_script"
        file_name = "%s.py" % script_name
        file_path = os.path.join(self.__base_dir, file_name)
        self.__temporary_resources.append(file_name)
        self.__temporary_resources.append(file_path)
        fd = file(file_path, "w")
        fd.write(str)
        fd.close()
        scripts = ScriptReader().read(self.__base_dir)
        self.assertTrue(1, len(scripts))
        script = [script for script in scripts if script.name == script_name][0]
        runner = Runner()
        runner.prepare()
        runner.run(script, None)
        self.assertTrue(os.path.exists(test_file_path))


    def test_run_scripts(self):
        script_name1 = "preinstall"
        script_name2 = "postinstall"
        script_file_name1 = "%s.py" % script_name1
        script_file_name2 = "%s.py" % script_name2
        
        self.__temporary_resources.append(script_file_name1)
        self.__temporary_resources.append(script_file_name2)
        
        str1 = "print 'first_one'"
        str2 = "import os\nif os.getenv('preinstall')!= 'first_one':\n\t raise 'Error'"
        
        fd1 = file(os.path.join(self.__base_dir, script_file_name1), "w")
        fd2 = file(os.path.join(self.__base_dir, script_file_name2), "w")
        fd1.write(str1)
        fd2.write(str2)
            
        fd1.close()
        fd2.close()

        scripts = ScriptReader().read(self.__base_dir)
        self.assertEqual(2, len(scripts))
        runner = Runner()
        runner.prepare()
        runner.run([script for script in scripts if script.name == script_name1][0], None)
        runner.run([script for script in scripts if script.name == script_name2][0], None)

    def test_run_script_with_exception(self):
        script_name1 = "preinstall"
        script_name2 = "postinstall"
        script_file_name1 = "%s.py" % script_name1
        script_file_name2 = "%s.py" % script_name2
        
        self.__temporary_resources.append(script_file_name1)
        self.__temporary_resources.append(script_file_name2)
        
        str1 = "print 'first_one'"
        str2 = "import os\nif os.getenv('pre_install')!= 'first_two':\n\t raise Exception('test error')"

        fd1 = file(os.path.join(self.__base_dir, script_file_name1), "w")
        fd2 = file(os.path.join(self.__base_dir, script_file_name2), "w")
        fd1.write(str1)
        fd2.write(str2)
            
        fd1.close()
        fd2.close()

        scripts = ScriptReader().read(self.__base_dir)
        self.assertEqual(2, len(scripts))
        
        runner = Runner()
        runner.prepare()
        script1 = [script for script in scripts if script.name == script_name1][0]
        runner.run(script1, None)
        script2 = [script for script in scripts if script.name == script_name2][0]
        self.assertRaises(RunnerError, Runner().run, script2, None)

    def test_run_error_script(self):
        script_name1 = "preinstall"
        script_file_name1 = "%s.py" % script_name1
               
        self.__temporary_resources.append(script_file_name1)
        
        str1 = "print os.listdir('.')"
        
        fd1 = file(os.path.join(self.__base_dir, script_file_name1), "w")
        fd1.write(str1) 
            
        fd1.close()

        scripts = ScriptReader().read(self.__base_dir)
        self.assertEqual(1, len(scripts))
        self.assertRaises(RunnerError, Runner().run, [script for script in scripts if script.name == script_name1][0], None)

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestRunner)
    TextTestRunner(verbosity=2).run(suite)
